/**
 * 
 */
package GUI;

/**
 * @author Avia Tam
 * @author Assaf Miron
 *
 */
//file CustomDialog.java    
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import Utils.Utils;

public class CustomDialog extends JDialog implements ActionListener,Runnable{  

	private static final long serialVersionUID = 1L;
	private JButton btnClose = null; 
	private Thread thread = null;  
	private int seconds = 5;
	private final int min = 0; //minimum number of seconds   
	private JPanel pnlButtons;
	private JPanel pnlIcon;
	private JPanel pnlData;

	public CustomDialog(String message, Integer seconds){
	    // Configure the Message for the Dialog
	    this(message);
	    // Configure the Time the Messsage will disapear
	    this.seconds = seconds;
	}

	public CustomDialog(String message){
		super(new JFrame(),true);//modal  
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		// Get the Parent Container
		Container cont = getContentPane();
		// Set the Container Layout
		cont.setLayout(new BorderLayout());
		// Add the Icon Panel
		cont.add(getIconPanel(),BorderLayout.WEST);
		// Add the Message Panel
		cont.add(getMessgePanel(message),BorderLayout.CENTER); 
		// Add the Buttons Panel
		cont.add(getButtonsPanel(),BorderLayout.SOUTH);
		
		// Set the Dialog in the Center of Screen
		this.setBounds(Utils.posCenterScreen(cont.getWidth(), cont.getHeight()));
		// Pack the Dialog - Make it Ready
		pack(); 
		
	}

	public void start(){
	    // Space the Message a little
		setPreferredSize(new Dimension((int) (getWidth()*2),(int) (getHeight()*1.2)));
		
		// Create a New Thread
		thread = new Thread(this);
		// Start the Thread
		thread.start();

		// Make the Dialog Visible
		setVisible(true);
	}
	
	/**
	 * @return The Buttons Panel
	 */
	private JPanel getButtonsPanel()
	{
		if(pnlButtons == null)
		{
		// Create a Buttons Panel
		pnlButtons = new JPanel();
		// Create the Close Button
		btnClose = new JButton("Close");
		// Add an Action Listener
		btnClose.addActionListener(this);  
		pnlButtons.add(Box.createGlue());  
		pnlButtons.add(btnClose);
		}
		return pnlButtons;
	}

	/**
	 * @return The Icon Panel
	 */
	private JPanel getIconPanel()
	{
		if(pnlIcon == null)
		{
			// Create an Icon for the Dialog
			JLabel image = new JLabel(Utils.getImageIcon("/info.png"));
			
			// Create an Icon Panel
			pnlIcon = new JPanel();
			// Add the Icon to the Panel
			pnlIcon.add(image);
		}
		return pnlIcon;
	}
	
	/**
	 * @param message The Message to display
	 * @return The Data of the Dialog
	 */
	private JPanel getMessgePanel(String message)
	{
		if(pnlData == null)
		{
			// Create the Main Message Label
			JLabel main_message = new JLabel(Utils.StringToHTML(message), JLabel.CENTER);

			// Create a Panel to Store all the Components
			pnlData = new JPanel(new BorderLayout());
			// Add the Message
			pnlData.add(main_message, BorderLayout.CENTER);
		}
		return pnlData;
	}
	
	
	public void actionPerformed(ActionEvent e){   
		setVisible(false); 
		dispose();
	}  

	public synchronized  void run(){
		while(seconds > min)
		{  
			seconds--; 
			setTitle("Auto closing in: "+seconds); 
			try{
			    wait(1000);
			}
			catch (InterruptedException exc)
			{ }
		}
		setVisible(false);
		dispose();
	}  

}//end  

